//
//  OrderViewController.m
//  AnnCar_user
//
//  Created by LiBenCan on 17/10/23.
//  Copyright © 2017年 LiBenCan. All rights reserved.
//

#import "OrderViewController.h"
#import "SGSegmentedControl.h"
#import "QBViewController.h"
#import "DJDViewController.h"
#import "DXCViewController.h"
#import "DYSViewController.h"
#import "YWCViewController.h"
@interface OrderViewController ()<UIScrollViewDelegate, SGSegmentedControlDelegate>
@property (nonatomic, strong) SGSegmentedControl *SG;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic,strong)UILabel *rightBarLabel;
@end
@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"我的订单"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 1.添加所有子控制器
    [self setupChildViewController];
    [self setupSegmentedControl];
    if (_isMyPush) {
        [standfault setObject:@"1" forKey:@"isMy"];
    }else
    {
        [standfault setObject:@"0" forKey:@"isMy"];
    }
}
- (void)setupSegmentedControl {
    
    NSArray *title_arr = @[@"全部", @"待接单", @"待洗车", @"待验收", @"已完成",];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * title_arr.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.bounces = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    QBViewController *oneVC = [[QBViewController alloc] init];
    [self.mainScrollView addSubview:oneVC.view];
    [self addChildViewController:oneVC];
    
    self.SG = [SGSegmentedControl segmentedControlWithFrame:CGRectMake(0,0, self.view.frame.size.width, 44) delegate:self segmentedControlType:(SGSegmentedControlTypeScroll) titleArr:title_arr];
    _SG.titleColorGradualChange = YES;
    //按钮颜色设置
    [self.SG setIndicatorColor:blueColor];
    [self.SG setTitleColorStateSelected:blueColor];
    [self.SG setTitleColorStateNormal:[UIColor blackColor]];
    [self.view addSubview:_SG];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    CGFloat offsetX = _intTypeIndex * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    [self.SG titleBtnSelectedWithScrollView:self.mainScrollView];
    [self showVc:_intTypeIndex];
    
}

- (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index {
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}

// 添加所有子控制器
- (void)setupChildViewController {
    //全部
    QBViewController *oneVC = [[QBViewController alloc] init];
    [self addChildViewController:oneVC];
    
    //待接单
    DJDViewController *twoVC = [[DJDViewController alloc] init];
    [self addChildViewController:twoVC];
    
    //待洗车
    DXCViewController *threeVC = [[DXCViewController alloc] init];
    [self addChildViewController:threeVC];
    
    //待验收
    DYSViewController *fourVC = [[DYSViewController alloc] init];
    [self addChildViewController:fourVC];
    
    //已完成
    YWCViewController *fiveVC = [[YWCViewController alloc] init];
    [self addChildViewController:fiveVC];
    
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    // 2.把对应的标题选中
    [self.SG titleBtnSelectedWithScrollView:scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.SG titleBtnColorGradualChangeScrollViewDidScroll:scrollView];
}


@end
